package shell.view.components;

import inabit.inference.InferenceNode;

import java.awt.Color;
import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
/**@author Rafael Rocha*/
public class NodeRenderer extends DefaultTreeCellRenderer{
	private static final long serialVersionUID = 1L;
	
	private String focusedVariableID;
	
	public void setFocusedVariable(String id) {
		this.focusedVariableID = id;
	}

	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean selected, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {
		super.getTreeCellRendererComponent(tree, value,
				selected, expanded, leaf, row, hasFocus);
		InferenceNode node = null;
		if(value instanceof InferenceNode) {
			node = (InferenceNode)value;
		}
		if(node != null){
			
			setNormalIcon(leaf);
			setForeground(Color.BLACK);
			if(!node.wasVerified()) {
				setIcon(new ImageIcon("rsc//nonVerified.png"));				
			}
			
			if(node.isConclusionNode()) {
				setBackgroundNonSelectionColor(new Color(200,220,255));
				setBackgroundSelectionColor(new Color(120,200,255));
			}else {
				setBackgroundNonSelectionColor(Color.WHITE);
				setBackgroundSelectionColor(new Color(200,200,200));
			}
			if(node.isInconsistencyNode()) {
				setBackgroundNonSelectionColor(new Color(255,200,200));
				setBackgroundSelectionColor(new Color(255,150,120));
			}
			if(node.isReferencedVariable(this.focusedVariableID)) {
				setBackgroundNonSelectionColor(new Color(200,255,120));
				setBackgroundSelectionColor(new Color(180,220,120));
			}
		}
		return this;
	}
	
	private void setNormalIcon(boolean leaf) {
		if(!leaf){
			setIcon(new ImageIcon("rsc//opened.png"));
		}else {
			setIcon(new ImageIcon("rsc//closed.png"));
		}
	}

}
